import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_project/config/app_bar_view.dart';
import 'package:flutter_project/config/theme_color.dart';
import 'package:flutter_project/help/help_model.dart';
import 'danmu_item_view.dart';

class HelpFeedPage extends StatefulWidget {
  const HelpFeedPage({super.key});

  @override
  State<HelpFeedPage> createState() => _HelpFeedPageState();
}

class _HelpFeedPageState extends State<HelpFeedPage> {
  List sectionList = ['看剧问题', '广告问题', '充值问题', '账号问题'];
  List rowList = [
    [
      '为什么短剧没更完/更新慢？',
      '剧集缺失/顺序错乱/音画不同频怎么办？',
      '如何开启倍速观看？',
      '为何会搜索到付费短剧？',
      '客户端闪退怎么办？',
      '客户端播放卡顿如何解决？',
      '如何查看观看历史/找到最近观看的剧集？',
    ],
    ['广告多/如何关闭广告？', '如何投诉虚假广告？', '如何避免下载广告宣传的应用？'],
    [
      '看点有效期问题',
      '账户里的看点或VIP丢失？',
      '是否可以申请退款问题？',
      '我已经付款了，为什么会员状态还没开通/看点没有到账？',
      '如何查看看点/会员充值记录?'
    ],
    ['如何注销个人账号？']
  ];
  List allList = [];

  double distance = 0.0;

  List<String> danmus = [
    '弹幕1',
    '弹幕2',
    '弹幕3',
    '弹幕4',
    '弹幕5',
  ];
  late Timer danmuTimer;

  @override
  Widget build(BuildContext context) {
    return CommonAppWidget.navAppBar(
      title: '帮助与反馈',
      bgColor: Colors.white,
      childWidget: Stack(
        children: [
          ListView.builder(
            itemCount: sectionList.length,
            itemBuilder: (BuildContext context, int index) {
              HelpModel model = allList[index];
              return getSectionView(sectionModel: model);
            },
          ),
          GridView.builder(
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  childAspectRatio: 2,
                  crossAxisSpacing: 10,
                  mainAxisSpacing: 10),
              itemCount: danmus.length,
              itemBuilder: (BuildContext context, int index) {
                return const DanmuItemView();
              }),
        ],
      ),
    );
  }

  Widget getSectionView({required HelpModel sectionModel}) {
    return Column(
      children: [
        Container(
          height: 45,
          padding: const EdgeInsets.symmetric(horizontal: 10),
          color: ThemeColor.mainBgColor,
          child: Row(
            children: [
              const Icon(
                Icons.menu_book_rounded,
                color: Colors.grey,
              ),
              const SizedBox(
                width: 10,
              ),
              Text(sectionModel.title)
            ],
          ),
        ),
        Column(
          children: getRowList(rowList: sectionModel.rowList),
        )
      ],
    );
  }

  List<Widget> getRowList({required List rowList}) {
    List<Widget> l = [];
    for (HelpRowModel element in rowList) {
      l.add(getRowItem(element));
    }

    return l;
  }

  Widget getRowItem(HelpRowModel model) {
    return GestureDetector(
      onTap: () {
        setState(() {
          model.isOpen = !model.isOpen;
        });
      },
      child: Container(
        key: ObjectKey(model),
        padding: const EdgeInsets.symmetric(horizontal: 10),
        color: Colors.white,
        child: Column(
          children: [
            const SizedBox(
              height: 10,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: Text(
                    model.name,
                    maxLines: 2,
                    style: const TextStyle(
                      fontSize: 14,
                    ),
                  ),
                ),
                Icon(
                  Icons.keyboard_arrow_down_outlined,
                  color: ThemeColor.lineColor,
                ),
              ],
            ),
            Visibility(
              visible: model.isOpen,
              child: Column(
                children: [
                  const Text(
                    '您好，短剧因为创意、拍摄、剪辑，上传需要大量时间，可能会出现看的比更新快的情况出现，请您耐心等待。若您不想等待，也可以优先选择剧场里已经完结的短剧观看哦～',
                    style: TextStyle(fontSize: 13),
                  ),
                  Row(
                    children: [
                      TextButton(
                        onPressed: () {},
                        child: const Row(
                          children: [
                            Icon(
                              Icons.thumb_down_outlined,
                              color: Colors.grey,
                              size: 15,
                            ),
                            Text(
                              '未解决',
                              style:
                                  TextStyle(color: Colors.grey, fontSize: 14),
                            ),
                          ],
                        ),
                      ),
                      TextButton(
                        onPressed: () {},
                        child: const Row(
                          children: [
                            Icon(
                              Icons.thumb_up_alt_outlined,
                              color: Colors.grey,
                              size: 15,
                            ),
                            Text(
                              '已解决',
                              style:
                                  TextStyle(color: Colors.grey, fontSize: 14),
                            ),
                          ],
                        ),
                      ),
                    ],
                  )
                ],
              ),
            ),
            const SizedBox(
              height: 10,
            ),
            Container(
              height: 1,
              color: ThemeColor.lineColor,
            ),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();

    for (int i = 0; i < sectionList.length; i++) {
      HelpModel helpModel = HelpModel();
      helpModel.title = sectionList[i];
      allList.add(helpModel);
    }

    for (int i = 0; i < allList.length; i++) {
      List<HelpRowModel> list = [];
      HelpModel helpModel = allList[i];
      List<String> nameList = rowList[i];
      for (String element in nameList) {
        HelpRowModel helpRowModel = HelpRowModel();
        helpRowModel.name = element;
        list.add(helpRowModel);
      }
      helpModel.rowList = list;
    }

    danmuTimer = Timer.periodic(const Duration(seconds: 5), (timer) {
      setState(() {
        danmus.add('新弹幕${danmus.length + 1}');
      });
    });
  }

  @override
  void dispose() {
    if (danmuTimer.isActive) {
      danmuTimer.cancel();
    }
    super.dispose();
  }
}
